package com.motorola.dp.jenkinsmonitor.service.util;

import java.io.File;

import org.springframework.stereotype.Service;

@Service("fileService")
public final class DefaultFileService implements FileService {

    private static final String EXTENSION_SEPARATOR = ".";

    public String getExtension(File file) {
        if (file == null || !file.isFile()) {
            throw new IllegalArgumentException("Invalid file");
        }
        String fullPath = file.getAbsolutePath();
        int dot = fullPath.lastIndexOf(EXTENSION_SEPARATOR);
        if (dot != -1) {
            return fullPath.substring(dot + 1);
        }
        return "";
    }

    public boolean isValidFile(File file, String extension) {
        if (file == null || !file.isFile()) {
            return false;
        } else if (!extension.equalsIgnoreCase(getExtension(file))) {
            return false;
        }
        return true;
    }

}
